﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.BLL;
using CRM.Entity;
using DP.Web.UI;

namespace CRM.Web
{
    public partial class LoginPage : Common.PageBase
    {
        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
        }

        T_USER_BLL bll = new T_USER_BLL();
        T_ROLE_BLL rBll = new T_ROLE_BLL();
        T_DEPARTMENT_BLL dBll = new T_DEPARTMENT_BLL();
        T_MARKETDEPARTMENT_BLL marketDepartmentBll = new T_MARKETDEPARTMENT_BLL();
        T_DEALER_BLL dealerBll = new T_DEALER_BLL();
        T_SUPPLIER_BLL supplierBll = new T_SUPPLIER_BLL();
        T_SERVICEPROVIDER_BLL serviceProviderBll = new T_SERVICEPROVIDER_BLL();
        
        V_ROLEMENU_BLL rmBll = new V_ROLEMENU_BLL();
        V_ROLEPERMISSION_BLL rpBll = new V_ROLEPERMISSION_BLL();
        private T_BUSINESSDIVISION_BLL businessdivisionBll = new T_BUSINESSDIVISION_BLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ibtnLogin.ImageUrl = base.ResourcesPath + "images/Login/login_ok.png";
                txtTelNo.Text = (Request.Cookies[StaticConstant.ExtCookies] == null) ? "" : Request.Cookies[StaticConstant.ExtCookies].Value.Trim();
            }
        }


        protected void txtLoginName_TextChanged(object sender, EventArgs e)
        {
            T_USER_Entity entity = bll.GetEntity("LOGINNAME", txtLoginName.Text.Trim());
            if (entity != null)
            {
                txtAgentID.Text = entity.SEATID;
            }
        }

        protected void ibtnLogin_Click(object sender, ImageClickEventArgs e)
        {
            string pwd = UserLoginInfo.PasswordEncryption(txtPassWord.Text.Trim());
            string loginName = txtLoginName.Text.Trim().Replace("'", "");
            string seatID = txtAgentID.Text.Trim();
            string tel = txtTelNo.Text.Trim();

            T_USER_Entity uEntity = bll.GetEntity("LOGINNAME", loginName);
            if (uEntity == null)
            {
                PageHelper.ShowMessage(UpdatePanel1, typeof(UpdatePanel), "登录名不存在！");
                return;
            }
            if (uEntity.STATUS.ToString() != ((int)Status.启用).ToString())
            {
                PageHelper.ShowMessage(UpdatePanel1, typeof(UpdatePanel), "账号已停用！");
                return;
            }
            if (uEntity.PASSWORD != pwd)
            {
                PageHelper.ShowMessage(UpdatePanel1, typeof(UpdatePanel), "你的密码不正确，请重新输入！");
                return;
            }
            uEntity.LOGINCOUNT = (uEntity.LOGINCOUNT == null) ? 1 : uEntity.LOGINCOUNT.Value + 1;
            uEntity.LASTLOGINTIME = DateTime.Now;
            uEntity.LASTLOGINIP = PageHelper.GetClientInfo().IP;
            bll.Update(uEntity);

            uEntity.EXT = txtTelNo.Text.Trim();
            UserLoginInfo loginInfo = new UserLoginInfo();
            loginInfo.UserEntity = uEntity;
            if (uEntity.ISSYSTEMADMIN.ToString() == ((int)IsAdmin.是).ToString())
            {
                loginInfo.DepartmentEntity = new T_DEPARTMENT_Entity();
                loginInfo.DepartmentEntity.DEPARTMENTNAME = "系统管理员";
                loginInfo.DepartmentEntity.DEPARTMENTID = 0;
                loginInfo.RoleEntity = new T_ROLE_Entity();
                loginInfo.RoleEntity.ROLENAME = "系统管理员";
                loginInfo.RoleEntity.ROLEID = 0;
                loginInfo.BusinessdivisionEntity = new T_BUSINESSDIVISION_Entity();
                loginInfo.BusinessdivisionEntity.BUSINESSDIVISIONCODE = "6";
                loginInfo.BusinessdivisionEntity.BUSINESSDIVISIONID = 4;
                loginInfo.BusinessdivisionEntity.BUSINESSDIVISIONNAME = "农业装备事业部";
            }
            else
            {
                switch (UserLoginInfo.GetUserType(loginInfo))
                {
                    case UserType.服务商:
                        {
                            loginInfo.ServiceProviderEntity = serviceProviderBll.GetEntity("SERVICEPROVIDERID", uEntity.DEPARTMENTID.ToString());
                        }
                        break;
                    case UserType.供应商:
                        {
                            loginInfo.SupplierEntity = supplierBll.GetEntity("SUPPLIERID", uEntity.DEPARTMENTID.ToString());
                        }
                        break;
                    case UserType.经销商:
                        {
                            loginInfo.DealerEntity = dealerBll.GetEntity("DEALERID", uEntity.DEPARTMENTID.ToString());
                        }
                        break;
                    case UserType.市场部:
                        {
                            loginInfo.MarketDepartmentEntity = marketDepartmentBll.GetEntity("MARKETDEPARTMENTID", uEntity.DEPARTMENTID.ToString());
                        }
                        break;
                    case UserType.中心部门:
                        {
                            loginInfo.DepartmentEntity = dBll.GetEntity("DEPARTMENTID", uEntity.DEPARTMENTID.ToString());
                        }
                        break;
                }
                loginInfo.RoleEntity = rBll.GetEntity("ROLEID", uEntity.ROLEID.ToString());
                loginInfo.BusinessdivisionEntity = businessdivisionBll.GetEntity("BUSINESSDIVISIONID", uEntity.BUSINESSDIVISIONID.ToString());
                loginInfo.RoleMenuList = rmBll.GetList(string.Format(" and ROLEID={0}", uEntity.ROLEID.ToString()), " SORTODER desc, MENUID asc ");
                loginInfo.RolePermissionList = rpBll.GetList(string.Format(" and ROLEID={0}", uEntity.ROLEID.ToString()), " AutoID asc");
            }
            loginInfo.ClientInfo = PageHelper.GetClientInfo(this.Page);
            loginInfo.LoginTime = DateTime.Now;
            UserLoginInfo.SetLoginInfo(loginInfo);
            UserLoginInfo.WriteLog(LogType.登录, "登录", "登录", "");

            Response.Cookies[StaticConstant.ExtCookies].Value = tel.Trim();
            Response.Cookies[StaticConstant.ExtCookies].Expires = DateTime.Now.AddYears(1);


            if (String.IsNullOrEmpty(seatID))
            {
                Response.Redirect(String.Format("~/Default.aspx?SEATID={0}&EXT={1}", seatID, tel));
            }
            else
            {
                Response.Redirect(String.Format("~/DefaultSoftPhone.aspx?SEATID={0}&EXT={1}", seatID, tel));
            }
        }
    }
}
